package com.school.sdk.haikang.alarm;

import cn.hutool.core.util.StrUtil;
import com.school.sdk.haikang.NetSDKDemo.HCNetSDK;
import com.school.sdk.haikang.helper.PostAlarmDataUtils;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;

@Slf4j
public class AlarmDataParse {

    public static void alarmDataHandle(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        String oxCommand = Integer.toHexString(lCommand);
        String sAlarmInfoIp = new String(pAlarmer.sDeviceIP).trim();

        log.info("[alarm-msg-start] ip[{}]报警类型[{}]", sAlarmInfoIp, oxCommand);

        switch (lCommand) {
            case HCNetSDK.COMM_ISAPI_ALARM: //ISAPI协议报警信息
                HCNetSDK.NET_DVR_ALARM_ISAPI_INFO struEventISAPI = new HCNetSDK.NET_DVR_ALARM_ISAPI_INFO();
                struEventISAPI.write();
                Pointer pEventISAPI = struEventISAPI.getPointer();
                pEventISAPI.write(0, pAlarmInfo.getByteArray(0, struEventISAPI.size()), 0, struEventISAPI.size());
                struEventISAPI.read();
                //报警数据类型：0- invalid，1- xml，2- json
                log.info("报警设备IP：" + sAlarmInfoIp + "：ISAPI协议报警信息, 数据格式:" + struEventISAPI.byDataType + ", 图片个数:" + struEventISAPI.byPicturesNumber);
                //报警数据打印
                HCNetSDK.BYTE_ARRAY m_strISAPIData = new HCNetSDK.BYTE_ARRAY(struEventISAPI.dwAlarmDataLen);
                m_strISAPIData.write();
                Pointer pPlateInfo = m_strISAPIData.getPointer();
                pPlateInfo.write(0, struEventISAPI.pAlarmData.getByteArray(0, m_strISAPIData.size()), 0, m_strISAPIData.size());
                m_strISAPIData.read();

                String codeStr = new String(m_strISAPIData.byValue).trim() + "\n";
                Integer alarmCode = null;
                if (codeStr.contains("<ruleType>temperatureSuddenIncrease</ruleType>")) {
                    alarmCode = PostAlarmDataUtils.fire_alarm_code;
                } else if (codeStr.contains("Objects Thrown From Buildings Detection")) {
                    alarmCode = PostAlarmDataUtils.throw_alarm_code;
                }
                //
                if (!ObjectUtils.isEmpty(alarmCode)) {
                    String picFilePath = "";
                    String picFilePath2 = "";
                    //图片数据保存 1张就行 火点获取2张
                    for (int i = 0; i < struEventISAPI.byPicturesNumber; i++) {
                        HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA struPicData = new HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA();
                        struPicData.write();
                        Pointer pPicData = struPicData.getPointer();
                        pPicData.write(0, struEventISAPI.pPicPackData.getByteArray(i * struPicData.size(), struPicData.size()), 0, struPicData.size());
                        struPicData.read();
                        String picFilePathUrl = PostAlarmDataUtils.getPicBaseName(lCommand);
                        if (i == 0) {
                            picFilePath = picFilePathUrl;
                        }else {
                            if (ObjectUtils.nullSafeEquals(PostAlarmDataUtils.fire_alarm_code, alarmCode)) {
                                picFilePath2 = picFilePathUrl;
                            }else {
                                break;
                            }
                        }
                        try {
                            FileOutputStream fout = new FileOutputStream(picFilePathUrl);
                            long offset = 0;
                            ByteBuffer buffers = struPicData.pPicData.getByteBuffer(offset, struPicData.dwPicLen);
                            byte[] bytes = new byte[struPicData.dwPicLen];
                            buffers.rewind();
                            buffers.get(bytes);
                            fout.write(bytes);
                            fout.close();
                        } catch (Exception e) {
                            picFilePath = null;
                            e.printStackTrace();
                        } finally {
                        }
                        //only need one , for-circle for prepare
                        if (i >= 1) {
                            break;
                        }
                    }
                    PostAlarmDataUtils.sendHaiKangMsg(sAlarmInfoIp, picFilePath, picFilePath2, alarmCode);

                }


                break;
            case HCNetSDK.COMM_THERMOMETRY_ALARM:  //温度报警信息
                HCNetSDK.NET_DVR_THERMOMETRY_ALARM struTemInfo = new HCNetSDK.NET_DVR_THERMOMETRY_ALARM();
                struTemInfo.write();
                Pointer pTemInfo = struTemInfo.getPointer();
                pTemInfo.write(0, pAlarmInfo.getByteArray(0, struTemInfo.size()), 0, struTemInfo.size());
                struTemInfo.read();
                String sThermAlarmInfo = "规则ID:" + struTemInfo.byRuleID + "预置点号：" + struTemInfo.wPresetNo + "报警等级：" + struTemInfo.byAlarmLevel + "报警类型：" +
                        struTemInfo.byAlarmType + "当前温度：" + struTemInfo.fCurrTemperature;
                log.info(sThermAlarmInfo);

                //可见光图片保存
                if ((struTemInfo.dwPicLen > 0) && (struTemInfo.byPicTransType == 0)) {
                    String picFilePath = PostAlarmDataUtils.getPicBaseName(lCommand);
                    String picFilePath2 = PostAlarmDataUtils.getPicBaseName(lCommand);
                    try {
                        FileOutputStream fout = new FileOutputStream(picFilePath);
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = struTemInfo.pPicBuff.getByteBuffer(offset, struTemInfo.dwPicLen);
                        byte[] bytes = new byte[struTemInfo.dwPicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();

                        FileOutputStream fout2 = new FileOutputStream(picFilePath2);
                        //将字节写入文件
                        ByteBuffer buffers2 = struTemInfo.pThermalPicBuff.getByteBuffer(offset, struTemInfo.dwThermalPicLen);
                        byte[] bytes2 = new byte[struTemInfo.dwThermalPicLen];
                        buffers2.rewind();
                        buffers2.get(bytes2);
                        fout2.write(bytes2);
                        fout2.close();
                    } catch (Exception e) {
                        picFilePath = null;
                        e.printStackTrace();
                    } finally {
                        PostAlarmDataUtils.sendHaiKangMsg(sAlarmInfoIp, picFilePath, picFilePath2, PostAlarmDataUtils.fire_alarm_code);
                    }
                }


                break;
            case HCNetSDK.COMM_FIREDETECTION_ALARM://烟火检测
                HCNetSDK.NET_DVR_FIREDETECTION_ALARM struFireDecAlarm = new HCNetSDK.NET_DVR_FIREDETECTION_ALARM();
                struFireDecAlarm.write();
                Pointer pFireDecAlarm = struFireDecAlarm.getPointer();
                pFireDecAlarm.write(0, pAlarmInfo.getByteArray(0, struFireDecAlarm.size()), 0, struFireDecAlarm.size());
                struFireDecAlarm.read();
                String sFireDecAlarmInfo = "绝对时间：" + struFireDecAlarm.dwAbsTime + ",报警子类型：" + struFireDecAlarm.byAlarmSubType + ",火点最高温度 :" +
                        struFireDecAlarm.wFireMaxTemperature + ",火点目标距离：" + struFireDecAlarm.wTargetDistance;
                log.info(sFireDecAlarmInfo);

                //可见光图片保存
                if ((struFireDecAlarm.dwVisiblePicLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
                    String picFilePath = PostAlarmDataUtils.getPicBaseName(lCommand);
                    try {
                        FileOutputStream fout = new FileOutputStream(picFilePath);
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = struFireDecAlarm.pVisiblePicBuf.getByteBuffer(offset, struFireDecAlarm.dwVisiblePicLen);
                        byte[] bytes = new byte[struFireDecAlarm.dwVisiblePicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();

                    } catch (Exception e) {
                        picFilePath = null;
                        e.printStackTrace();
                    } finally {
                        PostAlarmDataUtils.sendHaiKangMsg(sAlarmInfoIp, picFilePath, picFilePath, PostAlarmDataUtils.fire_alarm_code);
                    }
                }

                break;
            case HCNetSDK.COMM_THERMOMETRY_DIFF_ALARM: //温差检测报警
                HCNetSDK.NET_DVR_THERMOMETRY_DIFF_ALARM strThermDiffAlarm = new HCNetSDK.NET_DVR_THERMOMETRY_DIFF_ALARM();
                strThermDiffAlarm.write();
                Pointer pTemDiffInfo = strThermDiffAlarm.getPointer();
                pTemDiffInfo.write(0, pAlarmInfo.getByteArray(0, strThermDiffAlarm.size()), 0, strThermDiffAlarm.size());
                strThermDiffAlarm.read();
                String sThremDiffInfo = "通道号：" + strThermDiffAlarm.dwChannel + ",报警规则：" + strThermDiffAlarm.byAlarmRule + "，当前温差：" + strThermDiffAlarm.fCurTemperatureDiff;
                log.info(sThremDiffInfo);

                //可见光图片保存
                if ((strThermDiffAlarm.dwPicLen > 0) && (strThermDiffAlarm.byPicTransType == 0)) {
                    String picFilePath = PostAlarmDataUtils.getPicBaseName(lCommand);
                    String picFilePath2 = PostAlarmDataUtils.getPicBaseName(lCommand);
                    try {
                        FileOutputStream fout = new FileOutputStream(picFilePath);
                        long offset = 0;
                        ByteBuffer buffers = strThermDiffAlarm.pPicBuff.getByteBuffer(offset, strThermDiffAlarm.dwPicLen);
                        byte[] bytes = new byte[strThermDiffAlarm.dwPicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();

                        FileOutputStream fout2 = new FileOutputStream(picFilePath2);
                        //将字节写入文件
                        ByteBuffer buffers2 = strThermDiffAlarm.pThermalPicBuff.getByteBuffer(offset, strThermDiffAlarm.dwThermalPicLen);
                        byte[] bytes2 = new byte[strThermDiffAlarm.dwThermalPicLen];
                        buffers2.rewind();
                        buffers2.get(bytes2);
                        fout2.write(bytes2);
                        fout2.close();
                    } catch (Exception e) {
                        picFilePath = null;
                        e.printStackTrace();
                    } finally {
                        PostAlarmDataUtils.sendHaiKangMsg(sAlarmInfoIp, picFilePath, picFilePath2, PostAlarmDataUtils.fire_alarm_code);
                    }
                }
                break;
            default:
                log.info("默认报警类型" + Integer.toHexString(lCommand));
                break;
        }
        log.info("[alarm-msg-start] ip[{}]报警类型[{}]", sAlarmInfoIp, oxCommand);
    }
}
